<div class="modal-header">
	<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><li class="fa fa-remove"></li></button>
	<h5 class="modal-title">修改角色权限</h5>
</div>

<div class="modal-body">

	<form id="user-form" name="user-form" class="form-horizontal">
		<div class="box-body">
			<div class="col-md-12">
				<div class="form-group">
					<h2>【<label id="role1" name="role1" ></label>】</h2>
				</div>
			</div>
			<div class="col-md-12">
				<div class="col-sm-7">
					<h3>用户权限</h3>
					<div id="treeview-checkable" class=""></div>
				</div>
				<div class="col-sm-5">
					<h3>权限操作</h3>
					<div id="checkable-output"></div>
				</div>
			</div>

		</div>
		<!-- /.box-body -->
		<div class="box-footer text-right">
			<!--以下两种方式提交验证,根据所需选择-->
			<button type="button" class="btn btn-default" data-btn-type="cancel" data-dismiss="modal">取消</button>
			<button type="button" class="btn btn-primary" data-btn-type="save">提交</button>
		</div>

	</form>

</div>
<script>
	$(function() {
		var rolepower1 = sessionStorage.getItem('rolepower'); 
		powerEntity = JSON.parse(rolepower1);
		var name = powerEntity.name;
		var role2 = powerEntity.roleid;
		console.log(role2)
		$("#role1").html(name);
		var defaultData = [{
				text: '个人信息',
				href: '#parent1',
				tags: ['5'],
				nodes: [{
						text: '密码找回',
						href: '#child11',
						tags: ['0']
					},
					{
						text: '修改密码',
						href: '#child12',
						tags: ['0']
					},
					{
						text: '更新基本信息',
						href: '#child13',
						tags: ['0']
					},
					{
						text: '个人信息',
						href: '#child14',
						tags: ['0']
					},
					{
						text: '修改头像',
						href: '#child15',
						tags: ['0']
					}
				]
			},
			{
				text: '用户信息',
				href: '#parent2',
				tags: ['4'],
				nodes: [{
						text: '新增用户',
						href: '#child21',
						tags: ['0']
					},
					{
						text: '删除用户',
						href: '#child22',
						tags: ['0']
					},
					{
						text: '查询用户',
						href: '#child23',
						tags: ['0']
					},
					{
						text: '用户列表',
						href: '#child24',
						tags: ['0']
					}
				]
			}, {
				text: '角色信息',
				href: '#parent3',
				tags: ['3'],
				nodes: [{
						text: '查找角色',
						href: '#child31',
						tags: ['0']
					},
					{
						text: '更新角色',
						href: '#child32',
						tags: ['0']
					},
					{
						text: '查看角色',
						href: '#child33',
						tags: ['0']
					}
				]
			}, {
				text: '授权信息',
				href: '#parent4',
				tags: ['4'],
				nodes: [{
						text: '新增权限',
						href: '#child41',
						tags: ['0']
					},
					{
						text: '获取权限',
						href: '#child42',
						tags: ['0']
					},
					{
						text: '更新权限',
						href: '#child43',
						tags: ['0']
					},
					{
						text: '查看权限',
						href: '#child44',
						tags: ['0']
					}
				]
			}
		];
		var  nodeCheckedSilent  =  false;  
		function  nodeChecked (event,  node) {
			$('#checkable-output').prepend('<p name=' + node.text + '>' + node.text + '</p>');      
			if(nodeCheckedSilent) {          
				return;      
			}      
			nodeCheckedSilent  =  true;      
			checkAllParent(node);      
			checkAllSon(node);      
			nodeCheckedSilent  =  false;  
		}    
		var  nodeUncheckedSilent  =  false;  
		function  nodeUnchecked  (event,  node) { 
			$("#checkable-output").find("[name=" + node.text + "]").remove();     
			if(nodeUncheckedSilent)           return;      
			nodeUncheckedSilent  =  true;      
			uncheckAllParent(node);      
			uncheckAllSon(node);      
			nodeUncheckedSilent  =  false;  
		}    
		//选中全部父节点  
		function  checkAllParent(node) {      
			$('#treeview-checkable').treeview('checkNode', node.nodeId, {
				silent: true
			});      
			var  parentNode  =  $('#treeview-checkable').treeview('getParent', node.nodeId);      
			if(!("nodeId"  in  parentNode)) {          
				return;      
			} else {          
				checkAllParent(parentNode);      
			}  
		}  
		//取消全部父节点  
		function  uncheckAllParent(node) {      
			$('#treeview-checkable').treeview('uncheckNode', node.nodeId, {
				silent: true
			});      
			var  siblings  =  $('#treeview-checkable').treeview('getSiblings',  node.nodeId);      
			var  parentNode  =  $('#treeview-checkable').treeview('getParent', node.nodeId);      
			if(!("nodeId"  in  parentNode))  {          
				return;      
			}      
			var  isAllUnchecked  =  true;   //是否全部没选中  
			    
			for(var  i  in  siblings) {          
				if(siblings[i].state.checked) {              
					isAllUnchecked = false;              
					break;          
				}      
			}      
			if(isAllUnchecked) {          
				uncheckAllParent(parentNode);      
			}    
		}    
		//级联选中所有子节点  
		function  checkAllSon(node) {      
			$('#treeview-checkable').treeview('checkNode', node.nodeId, {
				silent: true
			});      
			if(node.nodes != null && node.nodes.length > 0) {          
				for(var  i  in  node.nodes) {              
					checkAllSon(node.nodes[i]);          
				}      
			}  
		}  
		//级联取消所有子节点  
		function  uncheckAllSon(node) {      
			$('#treeview-checkable').treeview('uncheckNode', node.nodeId, {
				silent: true
			});      
			if(node.nodes != null && node.nodes.length > 0) {          
				for(var  i  in  node.nodes) {              
					uncheckAllSon(node.nodes[i]);          
				}      
			}  
		}  
		var $checkableTree = $('#treeview-checkable').treeview({
			data: defaultData,
			showIcon: false,
			showCheckbox: true,
			onNodeChecked: nodeChecked,
			//			function(event, node) {
			//				$('#checkable-output').prepend('<p name=' + node.text + '>' + node.text + '</p>');
			//			},
			onNodeUnchecked: nodeUnchecked
			//			function(event, node) {
			//				$("#checkable-output").find("[name=" + node.text + "]").remove();
			//			}

		});

		var findCheckableNodess = function() {
			return $checkableTree.treeview('search', [$('#input-check-node').val(), {
				ignoreCase: false,
				exactMatch: false
			}]);
		};
		var checkableNodes = findCheckableNodess();

		// Check/uncheck/toggle nodes
		$('#input-check-node').on('keyup', function(e) {
			checkableNodes = findCheckableNodess();
			$('.check-node').prop('disabled', !(checkableNodes.length >= 1));
		});

		$('#btn-check-node.check-node').on('click', function(e) {
			$checkableTree.treeview('checkNode', [checkableNodes, {
				silent: $('#chk-check-silent').is(':checked')
			}]);
		});

		$('#btn-uncheck-node.check-node').on('click', function(e) {
			$checkableTree.treeview('uncheckNode', [checkableNodes, {
				silent: $('#chk-check-silent').is(':checked')
			}]);
		});

		$('#btn-toggle-checked.check-node').on('click', function(e) {
			$checkableTree.treeview('toggleNodeChecked', [checkableNodes, {
				silent: $('#chk-check-silent').is(':checked')
			}]);
		});

		// Check/uncheck all
		$('#btn-check-all').on('click', function(e) {
			$checkableTree.treeview('checkAll', {
				silent: $('#chk-check-silent').is(':checked')
			});
		});

		$('#btn-uncheck-all').on('click', function(e) {
			$checkableTree.treeview('uncheckAll', {
				silent: $('#chk-check-silent').is(':checked')
			});
		});

		//	});

		//			$id = $($("#tree").get(0).children[0].children[id]);
		//			$id.addClass('node-selected');
		$(".btn-primary").click(function(event) {
			event.preventDefault();
			$p = $("#checkable-output").find("p");
			//			console.log($p.length);
			var arr = [];
			for(var i = 0; i < $p.length; i++) {
				var p = $p[i].innerHTML;
				arr.push(p);
			}
			console.log(arr);
			var role1 = $("#role1").html();
			
			$.ajax({
				type: "post",
				url: "/power/updata",
				data: {
					rolename: role2,
					power: arr
				},
				success: function(data) {
					data = JSON.parse(data);
					console.log(data);
					//					if(data.code == 0) {
					//						alert("修改成功");
					//						location.href = "/power/list";
					//
					//						//						session.removeAttribute("userinformation");
					//						sessionStorage.removeItem("rolepower");
					//					} else {
					//						alert(data.message);
					//						event.preventDefault();
					//					}
				},
				error: function(er) {
					console.log(er);
				}
			});
		});
	});
</script>